package com.example.safehome;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.example.safehome.System;

import org.litepal.LitePal;

import java.util.*;

public class SystemActivity extends AppCompatActivity {

    private System system;
    private ImageView goBack;
    private TextView textViewInternetStatus;
    private TextView textViewSystemStatus;
    private TextView textViewUsername;
    private TextView textViewPhoneNumber;
    private TextView textViewFireNumber;
    private TextView textViewDelayTime;
    private TextView textViewIsAlarm;
    private TextView textViewIsBreakInRunning;
    private TextView textViewIsFireRunning;
    private TextView textViewIsCarbonRunning;
    private TextView textViewIsWaterRunning;
    private Button btnEditData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.safe_home_system);

        // 获得当前登录的用户
        LoginUser loginUser = new LoginUser();
        List<System> systemList = LitePal.where("hostName = ?", loginUser.getUsername()).find(System.class);

        // 如果没有则创建新用户，否则取列表的第一个用户
        if (systemList.size() == 0) {
            system = new System();
            system.setHostName(loginUser.getUsername());
            system.save();
        }
        else {
            system = (System) systemList.get(0);
        }

        // 显示数据
        displayInternetStatus();
        displaySystemStatus();
        displayUsername();
        displayPhoneNumber();
        displayFireNumber();
        displayDelayTime();
        displayAlarm();
        displaySensorStatus();

        // 编辑资料按钮
        btnEditData = (Button) findViewById(R.id.btn_edit_data);
        btnEditData.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(SystemActivity.this, EditDataActivity.class);
                startActivity(intent);
                finish();
            }
        });

        // 返回
        goBack = (ImageView) findViewById(R.id.image_safe_home_3);
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SystemActivity.this, SubSystemsChoosingActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    // 显示网络状态
    public void displayInternetStatus() {
        textViewInternetStatus = (TextView) findViewById(R.id.show_internet_status);
        textViewInternetStatus.setText("网络正常");
        textViewInternetStatus.setTextColor(Color.parseColor("#FF03AD18"));
    }

    // 显示系统状态
    public void displaySystemStatus() {
        textViewSystemStatus = (TextView) findViewById(R.id.show_system_status);
        textViewSystemStatus.setText("系统正常");
        textViewSystemStatus.setTextColor(Color.parseColor("#FF03AD18"));
    }

    // 显示用户名
    public void displayUsername() {
        textViewUsername = (TextView) findViewById(R.id.show_username);
        textViewUsername.setText(system.getHostName());
    }

    // 显示电话
    public void displayPhoneNumber() {
        textViewPhoneNumber = (TextView) findViewById(R.id.show_phone_number);
        textViewPhoneNumber.setText(system.getPhoneNumber());
    }

    // 显示火警电话
    public void displayFireNumber() {
        textViewFireNumber = (TextView) findViewById(R.id.show_fire_number);
        textViewFireNumber.setText(system.getFireCall());
    }

    // 显示警情响应时间
    public void displayDelayTime() {
        textViewDelayTime = (TextView) findViewById(R.id.show_delay_time);
        textViewDelayTime.setText(Double.toString(system.getDelayTime()) + "s");
    }

    // 显示是否有警情
    public void displayAlarm() {

        // 是否有警情（据传感器类判断）

        // 1.是否有非法入侵
        BreakInSensor breakInSensor = new BreakInSensor();
        boolean isBreakIn = breakInSensor.isBreakIn();
        Log.d("isBreakIn", Boolean.toString(isBreakIn));

        // 2.是否有火灾
        FireSensor fireSensor = new FireSensor();
        boolean isFire = fireSensor.isOnFire();

        // 3.是否一氧化碳超标
        CoDetector coDetector = new CoDetector();
        boolean isCarbon = coDetector.isExceeded;

        // 4.是否水位超标
        WaterInSensor waterInSensor = new WaterInSensor();
        boolean isWater = waterInSensor.isWaterIn();

        // 更新系统isAlarm
        if (isBreakIn == true || isFire == true
        || isCarbon == true || isWater == true) {
            system.setIsAlarm(true);
        }
        else {
            system.setIsAlarm(false);
        }

        if (system.getIsAlarm() == true) {

            // 延时报警
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    this.cancel();
                }
            }, (long)system.getDelayTime());

            // 仍为警报状态
            if (system.getIsAlarm() == true) {
                textViewIsAlarm = (TextView) findViewById(R.id.show_is_alarm);
                textViewIsAlarm.setText("已检测到警情");
                textViewIsAlarm.setTextColor(Color.parseColor("#FFFB0000"));
            }
        }
        else {
            textViewIsAlarm = (TextView) findViewById(R.id.show_is_alarm);
            textViewIsAlarm.setText("未检测到警情");
            textViewIsAlarm.setTextColor(Color.parseColor("#FF03AD18"));
        }
    }

    // 显示传感器工作状态（根据传感器类获取）
    public void displaySensorStatus() {

        // 1.非法入侵检测器
        BreakInSensor breakInSensor = new BreakInSensor();
        boolean isBreakInRunning = breakInSensor.isDetect();
        textViewIsBreakInRunning = (TextView) findViewById(R.id.show_break_in_detector);
        if (isBreakInRunning) {
            textViewIsBreakInRunning.setText("运行");
            textViewIsBreakInRunning.setTextColor(Color.parseColor("#FF03AD18"));
        }
        else {
            textViewIsBreakInRunning.setText("停止");
            textViewIsBreakInRunning.setTextColor(Color.parseColor("#FFF90D00"));
        }

        // 2.火灾检测器
        FireSensor fireSensor = new FireSensor();
        boolean isFireRunning = fireSensor.isDetect();
        textViewIsFireRunning = (TextView) findViewById(R.id.show_fire_detector);
        if (isFireRunning) {
            textViewIsFireRunning.setText("运行");
            textViewIsFireRunning.setTextColor(Color.parseColor("#FF03AD18"));
        }
        else {
            textViewIsFireRunning.setText("停止");
            textViewIsFireRunning.setTextColor(Color.parseColor("#FFF90D00"));
        }

        // 3.一氧化碳检测器
        CoDetector coDetector = new CoDetector();
        boolean isCarbonRunning = coDetector.isRunning;
        textViewIsCarbonRunning = (TextView) findViewById(R.id.show_carbon_monoxide_detector);
        if (isCarbonRunning) {
            textViewIsCarbonRunning.setText("运行");
            textViewIsCarbonRunning.setTextColor(Color.parseColor("#FF03AD18"));
        }
        else {
            textViewIsCarbonRunning.setText("停止");
            textViewIsCarbonRunning.setTextColor(Color.parseColor("#FFF90D00"));
        }

        // 4.水位检测器
        WaterInSensor waterInSensor = new WaterInSensor();
        boolean isWaterRunning = waterInSensor.isDetect();
        textViewIsWaterRunning = (TextView) findViewById(R.id.show_water_detector);
        if (isWaterRunning) {
            textViewIsWaterRunning.setText("运行");
            textViewIsWaterRunning.setTextColor(Color.parseColor("#FF03AD18"));
        }
        else {
            textViewIsWaterRunning.setText("停止");
            textViewIsWaterRunning.setTextColor(Color.parseColor("#FFF90D00"));
        }
    }
}
